﻿using System;
using System.Data;

namespace YawetagLib.Data.Xml
{
    public class Transaction : IDbTransaction
    {
        internal Transaction(Connection connection)
        {
            Connection = connection;
        }

        public void Commit()
        {
            Connection.ReleaseTransaction(true);
        }

        public void Rollback()
        {
            Connection.ReleaseTransaction(false);
        }

        public virtual void Close()
        {
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                if (disposing)
                {
                    ManagedResourceCleanup();
                }
                UnmanagedResourceCleanup();
                IsDisposed = true;
            }
        }

        protected virtual void ManagedResourceCleanup()
        {
            Close();
        }

        protected virtual void UnmanagedResourceCleanup()
        {
        }

        ~Transaction()
        {
            Dispose(false);
        }

        private Connection Connection { get; set; }

        IDbConnection IDbTransaction.Connection
        {
            get { return Connection; }
        }

        IsolationLevel IDbTransaction.IsolationLevel
        {
            get { return IsolationLevel.Unspecified; }
        }

        private bool IsDisposed { get; set; }
    }
}
